
#version 410

layout (location=0) in vec3 VertexPosition;
layout (location=3) in vec2 TexCoord0;
layout (location=1) in vec3 VertexNormal;
layout (location=2) in vec3 VertexColor;
layout (location=0) out vec2 vTexCoord;

layout (location=1) out vec3 vLightIntensity;

//uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat3 osg_NormalMatrix;
uniform mat4 osg_ModelViewMatrix;

void main()
{
    vec3 n = normalize( osg_NormalMatrix * VertexNormal);
    vec4 camCoords = osg_ModelViewMatrix * vec4(VertexPosition,1.0);

    vec3 ambient = vec3(0.1, 0.1, 0.1) * VertexColor;
    vec3 s = normalize(vec3(10.0f, 10.0, 10.0) - camCoords.xyz);
    float sDotN = max( dot(s,n), 0.0 );
    vec3 diffuse = vec3(1.0, 1.0, 1.0) * vec3(1.0, 1.0, 1.0) * sDotN;
    vec3 spec = vec3(0.0);

    if( sDotN > 0.0 ) 
    {
        vec3 v = normalize(-camCoords.xyz);
        vec3 r = reflect( -s, n );
        spec = vec3(1.0, 1.0, 1.0) * vec3(1.0, 1.0, 1.0) *
                pow( max( dot(r,v), 0.0 ), 16);
    }

    vLightIntensity = ambient + diffuse + spec;

    vTexCoord = TexCoord0;
    gl_Position = osg_ModelViewProjectionMatrix * vec4(VertexPosition,1.0);
    //gl_Position = gl_ModelViewProjectionMatrix * vec4(VertexPosition,1.0);

}
